package com.electromagnetic.industry.software.manage.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.electromagnetic.industry.software.manage.pojo.models.Role;
import com.electromagnetic.industry.software.manage.pojo.req.RoleDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface RoleMapper extends BaseMapper<Role> {


    @Select("<script>" +
            "SELECT " +
            "    r.id AS roleId, " +
            "    r.role_name AS roleName, " +
            "    r.role_desc AS roleDesc, " +
            "    GROUP_CONCAT(DISTINCT f.file_name) AS dataScope, " +
            "    GROUP_CONCAT(DISTINCT rp.permission_code) AS allowedActions, " +
            "    CASE " +
            "    WHEN MAX(u.is_activated) = 1 THEN 1 " +
            "    ELSE 0 " +
            "    END AS isBind " +
            "FROM " +
            "    ed_role r " +
            "LEFT JOIN " +
            "    ed_role_permission rp ON r.id = rp.role_id " +
            "LEFT JOIN " +
            "    ed_file_info f ON rp.file_id = f.id " +
            "LEFT JOIN" +
            "    ed_user_role ur ON r.id = ur.role_id " +
            "LEFT JOIN" +
            "    ed_users u ON ur.user_id = u.id " +
            "WHERE " +
            " 1=1 " +
            "<if test=\"ew != null and ew.sqlSegment != null and ew.sqlSegment != ''\">" +
            "   AND ${ew.sqlSegment} " +
            "</if>" +
            "GROUP BY " +
            "    r.id" +
            "</script>")
    Page<RoleDTO> getPageRoleDTO(Page<RoleDTO> page, @Param(Constants.WRAPPER) QueryWrapper<Role> queryWrapper);
}
